def get_or_create_spark_session():
    """

    Returns:
        a ``SparkSession``
    """
    from pyspark.sql import SparkSession
    return SparkSession.builder.enableHiveSupport().getOrCreate()


def get_spark_user_name():
    spark = get_or_create_spark_session()
    sc = spark.sparkContext
    return sc.sparkUser()


def get_proc_user_name():
    import psutil
    p = psutil.Process()
    with p.oneshot():
        return p.username()


def get_session_id():
    """
    Returns:
        the livy session id

    Notes:
        It seems impossible to obtain a session id via client python code
        for jupyter.
    """
    # TODO: give a real implementation

    return 'N/A'


def get_application_id():
    """
    Returns:
        the spark application id
    """
    sc = get_or_create_spark_session().sparkContext
    return sc.applicationId


def get_global_log_path():
    return "/tmp/gezhi_{}/".format(get_proc_user_name())


def get_package_version():
    import gai.version
    return gai.version.__version__


def get_extra_params():
    import gai.version
    return {
        "user_name"     : get_spark_user_name(),
        "client_version": gai.version.__version__
    }
